//
//  KsDetailViewController.swift
//  iKinds
//
//  Created by kinds on 2016/11/18.
//  Copyright © 2016年 Razeware LLC. All rights reserved.
//

import UIKit

class KsDetailViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
	
	var ksMain:KsMain?
	
	@IBOutlet weak var labTitle: UILabel!
	@IBOutlet weak var labRange: UILabel!
	@IBOutlet weak var labClassify: UILabel!
	
	@IBOutlet weak var labBody: UILabel!
	@IBOutlet weak var labExceptions: UILabel!
	@IBOutlet weak var labSignature: UILabel!
	@IBOutlet weak var tableView: UITableView!
	
	var feedbacks = [KsFeedback]()
	weak var containerVC:ContainerViewController!
	
	/*
	init(containerVC:ContainerViewController){
		self.containerVC = containerVC
		super.init(nibName: nil, bundle: nil)
	}
	
	required init?(coder aDecoder: NSCoder) {
		fatalError("init(coder:) has not been implemented")
	}*/

    override func viewDidLoad() {
        super.viewDidLoad()
		
		if let ks = ksMain {
			labTitle.text = "\(ks.type ?? "")\(ks.title!)"
			labRange.text = "\(standardDateFor(date: ks.dateBegin!))-\(standardDateFor(date: ks.dateEnd!))"
			labClassify.text = ks.classify
			labBody.text = ks.text
			if ks.exception != nil{
				labExceptions.text = "E \(ks.exception!)"
			}else{
				labExceptions.text = ""
			}
			
			labSignature.text = ">z"
			labRange.adjustsFontSizeToFitWidth = true
			
			feedbacks = containerVC.getFeedbackFor(ks: ks) ?? [KsFeedback]()
			//feedbacks = ks.feedbacks?.allObjects as! [KsFeedback]
			//let fetchRequest:NSFetchRequest<KsFeedback> = KsFeedback.fetchRequest()
			//fetchRequest.predicate = NSPredicate(format: "main == %@", ks)
			
		}
		
		tableView.delegate = self
		tableView.dataSource = self
    }
	
	override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
		let nav = segue.destination as! UINavigationController
		let addKsVC = nav.viewControllers.first as! AddKsViewController
		addKsVC.containerVC = containerVC
		addKsVC.enterByPush = false
	}

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
	
	// MARK: - TableView
	
	func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
		let cell = tableView.dequeueReusableCell(withIdentifier: "KsFeedbackCell", for: indexPath)
		let fb = feedbacks[indexPath.row]
		cell.textLabel?.text = fb.text
		cell.detailTextLabel?.text = fb.date.description
		return cell
	}
	
	func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
		return feedbacks.count
	}
}
